const { localUrl } = require("../../utils/util");

// pages/label/label.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      current:1,
      limit:10,
      row:{},
      show: false,
      active: 0,
      activetab:0,
      imgs: [],
      imagesUrl:[]
  },
  navigate:function(e){
    console.log("e",e.currentTarget.dataset.id)

      wx.navigateTo({
        url: '/pages/detail/detail?id='+e.currentTarget.dataset.id,
        
      })
  },
 
   // 上传图片
   chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    var imgarr= this.data.imagesUrl;
    if (imgs.length >= 9) {
     this.setData({
      lenMore: 1
     });
     setTimeout(function () {
      that.setData({
       lenMore: 0
      });
     }, 2500);
     return false;
    }
    wx.chooseImage({
     // count: 1, // 默认9
     sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
     sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
     success: function (res) {
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
      var tempFilePaths = res.tempFilePaths;
      var imgs = that.data.imgs;
      // console.log(tempFilePaths + '----');
      for (var i = 0; i < tempFilePaths.length; i++) {
       if (imgs.length >= 9) {
        that.setData({
         imgs: imgs
        });
        return false;
       } else {
        imgs.push(tempFilePaths[i]);
       }
      }
      // console.log(imgs);
      that.setData({
       imgs: imgs
      });


      
        wx.uploadFile({

            url: localUrl+'/oss/label', //接受图片的接口地址
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
                'user': 'test'
            },
            success (res){
                console.log(res.data);
                var imginfo=JSON.parse(res.data)
                console.log("img",imginfo);
                imgarr.push(imginfo.data.url)
                that.setData({
                  imagesUrl:imgarr
                })
             
          
                //do something

            }
        })

     }
    });
   },
   // 删除图片
   deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    var imgarr=this.data.imagesUrl;
    var that= this
    imgs.splice(index, 1);
    console.log(imgarr[index])
    //删除图片
    wx.request({
      url: localUrl+'/oss/deleteFile',
      method:'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        fileUrl:imgarr[index]
      },
      success(res){
        console.log("删除图片",imgarr[index])
        console.log("删除图片",res)
      }
    })
    imgarr.splice(index,1);
    this.setData({
     imgs: imgs,
    imagesUrl:imgarr
    });
   },
   // 预览图片
   previewImg: function (e) {
     //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
     //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
     //当前显示图片
     current: imgs[index],
     //所有图片
     urls: imgs
    })
   },
  showPopup() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  onChangetab(e){
    wx.showToast({
      title: `切换到标签 ${e.detail.name}`,
      icon: 'none',
    });
  },
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    if (event.detail==1) {
      this.setData({ show: true });
    }
    if(event.detail==3){
      wx.navigateTo({
        url: '/pages/user/user',
        
      })
    }

    this.setData({ active: event.detail });
    
  

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this
     var token =wx.getStorageSync('token')
     if (token=='') {
       wx.showToast({
         title: '登陆失败',
         icon:'error'
       })
      wx.navigateTo({
        url: '/pages/login/login',
      })
     
   }else{
    wx.request({
      url: localUrl+'/ucenter/member/getMemberInfo',
      method:'GET',
      header:{
       'content-type': 'application/json', // 默认值
        'token':wx.getStorageSync('token')
      },
      
      success(res){
        console.log("token",res)
        wx.request({
          url: localUrl+'/ucenter/member/getMemberInfo',
          method:'GET',
          header:{
           'content-type': 'application/json', // 默认值
            'token':wx.getStorageSync('token')
          },
          
          success(res){
            console.log("token",res)
            that.setData({
              userInfo:res.data.data.userInfo
            })
          }
        })
      }
    })
   }

  
  },
  remarkInputAction:function(options){
 //获取输入框输入的内容
 let value = options.detail.value;
 console.log("输入框输入的内容是 " + value)
 this.setData({
   content:options.detail.value
 })
  },
  addLabel(){
    var that =this
    wx.request({
      url: localUrl+'/label/add',
      method:'POST',
      data:{
        content: this.data.content,
        "state": "string",
        "type": "string",
        "userId": this.data.userInfo.id
      },
      success(res){
        console.log(res)
        var label_id =res.data.data.label_id
        wx.request({
          url: localUrl+'/label/img/addImgList',
          method:'POST',
          data:{
            imgList: that.data.imagesUrl,
            labelId:label_id
          },
          header:{
            'content-type': 'application/x-www-form-urlencoded'
          },
          success(res){
            console.log("带图片上传",res)
          }
        })
      }
    })
    wx.navigateTo({
      url: '/pages/label/label',
     
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
    wx.request({
      url: localUrl+'/label/pageLabel/'+this.data.current+"/"+this.data.limit,
      method:'GET',
    
      success(res){
        that.setData({
          row:res.data.data.finalList
        })
        console.log(res)
      }
      
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showToast({
      title: '加载中',
      icon:'loading'
    })
    var that =this
      this.setData({
        current:that.data.current+1
      })
      wx.request({
        url: localUrl+'/label/pageLabel/'+that.data.current+"/"+that.data.limit,
        method:'GET',
        success(res){
        //   var row =that.data.finalList;
        //  var res_final= res.data.data.finalList
        //   var finaList =row.concat(res_final)
        console.log(res)
          that.setData({
           row:that.data.row.concat(res.data.data.finalList)
          })
         
        }
        
      })
      
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})